﻿
/**
 * linkbutton - jQuery xui
 *
 * Licensed under the GPL:
 *   http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2013 xjb [ beymy.en@gmail.com ]
 *
 */
(function($) {

    function buildButton(target) {
        var opts = $.data(target, 'linkbutton').options;
        var t = $(target).empty();

        t.addClass('l-btn').removeClass('l-btn-plain');
        t.removeClass('l-btn-small l-btn-medium l-btn-large').addClass('l-btn-' + opts.size);
        if (opts.plain) {
            t.addClass('l-btn-plain')
        }
 
        t.attr('id', opts.id || '');

        var inner = $('<span class="l-btn-left"></span>').appendTo(t);
        if (opts.text) {
            $('<span class="l-btn-text"></span>').html(opts.text).appendTo(inner);
        } else {
            $('<span class="l-btn-text l-btn-empty">&nbsp;</span>').appendTo(inner);
        }
        if (opts.iconCls) {
            $('<span class="l-btn-icon">&nbsp;</span>').addClass(opts.iconCls).appendTo(inner);
            inner.addClass('l-btn-icon-' + opts.iconAlign);
        }

        t.unbind('.linkbutton').bind('focus.linkbutton', function() {
            if (!opts.disabled) {
                $(this).addClass('l-btn-focus');
            }
        }).bind('blur.linkbutton', function() {
            $(this).removeClass('l-btn-focus');
        }).bind('click.linkbutton', function() {
            if (!opts.disabled) {
                opts.onClick.call(this);
            }
            return false;
        });

        setDisabled(target, opts.disabled);
    }

    function setDisabled(target, disabled) {
        var state = $.data(target, 'linkbutton');
        var opts = state.options;
        $(target).removeClass('l-btn-disabled l-btn-plain-disabled');
        if (disabled) {
            opts.disabled = true;
            var href = $(target).attr('href');
            if (href) {
                state.href = href;
                $(target).attr('href', 'javascript:void(0)');
            }
            if (target.onclick) {
                state.onclick = target.onclick;
                target.onclick = null;
            }
            opts.plain ? $(target).addClass('l-btn-disabled l-btn-plain-disabled') : $(target).addClass('l-btn-disabled');
        } else {
            opts.disabled = false;
            if (state.href) {
                $(target).attr('href', state.href);
            }
            if (state.onclick) {
                target.onclick = state.onclick;
            }
        }
    }

    $.fn.linkbutton = function(options, param) {
        if (typeof options == 'string') {
            return $.fn.linkbutton.methods[options](this, param);
        }

        options = options || {};
        return this.each(function() {
            var state = $.data(this, 'linkbutton');
            if (state) {
                $.extend(state.options, options);
            } else {
                $.data(this, 'linkbutton', {
                    options: $.extend({}, $.fn.linkbutton.defaults, $.fn.linkbutton.parseOptions(this), options)
                });
                $(this).removeAttr('disabled');
            }

            buildButton(this);
        });
    };

    $.fn.linkbutton.methods = {
        options: function(jq) {
            return $.data(jq[0], 'linkbutton').options;
        },
        enable: function(jq) {
            return jq.each(function() {
                setDisabled(this, false);
            });
        },
        disable: function(jq) {
            return jq.each(function() {
                setDisabled(this, true);
            });
        }
    };

    $.fn.linkbutton.parseOptions = function(target) {
        var t = $(target);
        return $.extend({}, $.parser.parseOptions(target, ['id', 'iconCls', 'iconAlign', 'size', {
            plain: 'boolean'
        }]), {
            disabled: (t.attr('disabled') ? true : undefined),
            text: $.trim(t.html()),
            iconCls: (t.attr('icon') || t.attr('iconCls'))
        });
    };

    $.fn.linkbutton.defaults = {
        id: null,
        disabled: false,
        plain: false,
        text: '',
        iconCls: null,
        iconAlign: 'left',
        size: 'small', // small,medium,large
        onClick: function() {}
    };

})(jQuery);
